
COBJECTS = $(filter %${XOBJ},$(patsubst %.c,${OBJDIR}/%${XOBJ},$(notdir $(C_SOURCES))))
CPPOBJECTS = $(filter %${XOBJ},$(patsubst %.cpp,${OBJDIR}/%${XOBJ},$(notdir $(CXX_SOURCES))))
CUOBJECTS = $(patsubst %.cu,${OBJDIR}/%${XOBJ},${CU_SOURCES})
#EXEOBJECTS = $(patsubst %.cpp,${OBJDIR}/%${XOBJ},${CXX_EXE_SOURCES})
C_EXEOBJECTS = $(patsubst %.c,${OBJDIR}/%${XOBJ},$(notdir $(C_EXE_SOURCES)))
EXEOBJECTS = $(patsubst %.cpp,${OBJDIR}/%${XOBJ},$(notdir $(CXX_EXE_SOURCES)))
#EXECUTABLES = $(patsubst %.cpp,${BINDIR}/%${XEXE},${CXX_EXE_SOURCES})
EXECUTABLES = $(patsubst %.c,${BINDIR}/%${XEXE},$(notdir $(C_EXE_SOURCES)))
EXECUTABLES += $(patsubst %.cpp,${BINDIR}/%${XEXE},$(notdir $(CXX_EXE_SOURCES)))
C_DYNOBJECTS = $(patsubst %.c,${OBJDIR}/%${XOBJ},$(notdir $(C_DLL_SOURCES)))
DYNOBJECTS = $(patsubst %.cpp,${OBJDIR}/%${XOBJ},$(notdir $(CXX_DLL_SOURCES)))
DYN_LIBS = $(patsubst %.c,${BINDIR}/%${XDLL},$(notdir $(C_DLL_SOURCES)))
DYN_LIBS += $(patsubst %.cpp,${BINDIR}/%${XDLL},$(notdir $(CXX_DLL_SOURCES)))
DEPFILES = $(patsubst %,${OBJDIR}/%.dep,${C_SOURCES} ${CXX_SOURCES} ${CU_SOURCES} ${CXX_EXE_SOURCES})

${COBJECTS}: ${OBJDIR}/%${XOBJ}: %.c
	${CC} ${CFLAGS} ${COUT}$@ $<

${CPPOBJECTS}: ${OBJDIR}/%${XOBJ}: %.cpp
	${CC} ${CFLAGS} ${COUT}$@ $<

${CUOBJECTS}: ${OBJDIR}/%${XOBJ}: %.cu
	${NVCC} ${CUFLAGS} ${CUOUT}$@ $<

${C_EXEOBJECTS}: ${OBJDIR}/%${XOBJ}: %.c
	${CC} ${CFLAGS} ${COUT}$@ $^

${EXEOBJECTS}: ${OBJDIR}/%${XOBJ}: %.cpp
	${CC} ${CFLAGS} ${COUT}$@ $^

${EXECUTABLES}: ${BINDIR}/%${XEXE}: ${OBJDIR}/%${XOBJ} ${COBJECTS} ${CPPOBJECTS} ${CUOBJECTS}
	${LINK} ${LFLAGS} ${LOUT}$@ $^
	
${C_DYNOBJECTS}: ${OBJDIR}/%${XOBJ}: %.c
	${CC} ${CFLAGS} ${COUT}$@ $^

${DYNOBJECTS}: ${OBJDIR}/%${XOBJ}: %.cpp
	${CC} ${CFLAGS} ${COUT}$@ $^
	
${DYN_LIBS}: ${BINDIR}/%${XDLL}: ${OBJDIR}/%${XOBJ} ${COBJECTS} ${CPPOBJECTS} ${CUOBJECTS}
	${LINK} ${DLLFLAGS} ${DLLOUT}$@ $^
#${LINK} ${DLLFLAGS}xattr${XDLL} ${DLLOUT} "libxattr"
#${LINK} ${DLLFLAGS},-soname,libctest.so.1 ${DLLOUT} libctest.so.1.0
#-shared -Wl,-soname=xattr.so -o "libxattr"

.PHONY:
MAKEDIRS:
	@if [ ! -d bin ]; then mkdir bin; fi
	@if [ ! -d bin/${ARCH} ]; then mkdir bin/${ARCH}; fi
	@if [ ! -d ${BINDIR} ]; then mkdir ${BINDIR}; fi

.PHONY:
clean:
	rm -f ${COBJECTS} ${CPPOBJECTS} ${CUOBJECTS} ${C_EXEOBJECTS} ${EXEOBJECTS} ${EXECUTABLES} ${DYN_LIBS} ${CLEAN_TARGETS} ${DEPFILES}

.PHONY:
deps: MAKEDIRS CLEAN_DEPS ${DEPFILES}

.PHONY:
CLEAN_DEPS:
	rm -f ${DEPFILES}

.PHONY:
${DEPFILES}: ${OBJDIR}/%.dep: %
	g++ -x c++ $< -MM $(addprefix -I",$(addsuffix ",${INCLUDE_PATHS})) -MT $(basename $(basename $@))${XOBJ} > $(@)

-include ${OBJDIR}/*.dep
